#include "co4107.h"
#include "st7026.h"
#include <string.h>
#include "stm32f1xx_hal.h"


unsigned char start = 0;
unsigned char dispBusy = 0;

void powerOn(void) {

}

void powerOff(void) {

}

void setDispBusy(unsigned char state) {
	if(state) {
		dispBusy = 1;
		setULed(1);
	} else {
		dispBusy = 0;
		setULed(0);
	}
}

unsigned char getDispBusy(void) {
	return dispBusy;
}


unsigned char pause = 0;

void keyPause() {
	for(uint16_t i = 0; i < 100; i++) {
		while(pause);
		HAL_Delay(10);
	}
}

const char* IconTable[] = {
    "1A", "1B", "3F", "3A", "4F", "4A", "5F", "5A", "6F", "6A", "P11", "M4", "M5",  "",   "2C", "N5", "T1", "T2", "P7", "2B", "",   "S0",
    "1F", "1G", "3G", "3B", "4G", "4B", "5G", "5B", "6G", "6B", "P10", "M3", "M6",  "M0", "N6", "N0", "T0", "T3", "P6", "2A", "",   "S1",
    "1E", "1C", "3E", "3C", "4E", "4C", "5E", "5C", "6E", "6C", "P9",  "M2", "M7",  "M8", "N7", "N4", "N1", "T4", "P5", "P3", "",   "S2",
    "1D", "DP", "2F", "3D", "2G", "4D", "2E", "5D", "2D", "6D", "P8",  "M1", "M10", "M9", "N8", "N3", "N2", "P2", "P4", "P1", "S4", "S3"
};


void drawIcon(const char* iconName, unsigned char state) {
    unsigned char seg = 0;
    unsigned char com = 0;
    for(seg = 0; seg < 22; seg++){
        for(com = 0; com < 4; com++ ) {
            if (strcmp(iconName, IconTable[com*22+seg]) == 0) {
                st7026_drawPiexl(seg, com, state);
				break;
            }
        }
    }
}


void flashULED(void) {
	setULed(1);
	HAL_Delay(100);
	setULed(0);
}

void co4107_test(void) {
	
	HAL_GPIO_WritePin(LCM_RST_GPIO_Port, LCM_RST_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(SPI1_MOSI_GPIO_Port, SPI1_MOSI_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(SPI1_SCK_GPIO_Port, SPI1_SCK_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
	setVLed(0);
	setVdd(0);
	start = 0;
	
	while(start == 0) {
		HAL_Delay(10);
	}
	
	setVLed(1);
	
	HAL_GPIO_WritePin(LCM_RST_GPIO_Port, LCM_RST_Pin, GPIO_PIN_RESET);
    HAL_Delay(50);

    setVdd(1);
    HAL_Delay(50);

    HAL_GPIO_WritePin(LCM_RST_GPIO_Port, LCM_RST_Pin, GPIO_PIN_SET);
    HAL_Delay(50);

	st7026_init();
	
	
	setDispBusy(1);
    st7026_fillByte(0xff); 
	setDispBusy(0);
	keyPause();
	
	
	#ifdef DEMO
	setDispBusy(1);
	const char* co4107_demo[49] = {
		"S1", "S2", "S3", "S4", 
		"P1", "P2", "T0", 
		"T0", "T2", "T3", "T4", 
		"N0", "N1", "N2", "N3", "N4", "N5", "N6", "N7", "N8",
		"1A", "1B", "1C", "1D", "1E", "1F", "1G",
		"2A", "2B", "2C", "2D", "2E", "2F", "2G",
		"P9", "P10", 
		"M0", "M1", "M9", "M10", 
		"M4", "M5", "M6", 
	};
    st7026_fillByte(0x00); 
	//bufferInit();
    for(uint8_t i = 0; i < 43; i++) {
        drawIcon(co4107_demo[i], ON);
    }
	setDispBusy(0);
    keyPause();
	#endif //DEMO
	
	setDispBusy(1);
	const char* co4107_test1[49] = {
		"S0", "S4", "P1", "T0", "T4", "P5", 
		"N1", "N0", "N8",
		"1A", "1B", "1C", "1D", "1E", "1F",
		"2A", "2B", "2C", "2D", "2E", "2F",
		"3A", "3B", "3C", "3D", "3E", "3F",
		"4A", "4B", "4C", "4D", "4E", "4F",
		"5A", "5B", "5C", "5D", "5E", "5F",
		"6A", "6B", "6C", "6D", "6E", "6F",
		"P8", "M0", "M2", "M8",
	};
    st7026_fillByte(0x00); 
	//bufferInit();
    for(uint8_t i = 0; i < 49; i++) {
        drawIcon(co4107_test1[i], ON);
    }
	setDispBusy(0);
    keyPause();
	
    setDispBusy(1);
	const char* co4107_test2[40] = {
		"S1", "P2", "T1", "P6", 
		"N2", "N7",
		"1A", "1B", "1D", "1E", "1G",
		"2A", "2B", "2D", "2E", "2G",
		"3A", "3B", "3D", "3E", "3G",
		"4A", "4B", "4D", "4E", "4G",
		"5A", "5B", "5D", "5E", "5G",
		"6A", "6B", "6D", "6E", "6G",
		"P9", 
		"M3", "M7", "M9",
	};
    st7026_fillByte(0x00); 
	//bufferInit();
    for(uint8_t i = 0; i < 40; i++) {
        drawIcon(co4107_test2[i], ON);
    }
	setDispBusy(0);
    keyPause();
	
	setDispBusy(1);
	const char* co4107_test3[34] = {
		"S2", "T2", "P3", "P7",
		"N3", "N6",
		"1B", "1C", "1F", "1G",
		"2B", "2C", "2F", "2G",
		"3B", "3C", "3F", "3G",
		"4B", "4C", "4F", "4G",
		"5B", "5C", "5F", "5G",
		"6B", "6C", "6F", "6G",
		"P10", 
		"M4", "M6", "M10",
	};
    st7026_fillByte(0x00); 
	//bufferInit();
    for(uint8_t i = 0; i < 34; i++) {
        drawIcon(co4107_test3[i], ON);
    }
	setDispBusy(0);
    keyPause();

	setDispBusy(1);
	const char* co4107_test4[28] = {
		"S3", "T4", "P4",
		"N4", "N5", 
		"1A", "1B", "1C", 
		"2A", "2B", "2C", 
		"3A", "3B", "3C", 
		"4A", "4B", "4C", 
		"5A", "5B", "5C", 
		"6A", "6B", "6C", 
		"DP", "P11", 
		"M1", "M5",
	};
    st7026_fillByte(0x00); 
	//bufferInit();
    for(uint8_t i = 0; i < 28; i++) {
        drawIcon(co4107_test4[i], ON);
    }
	setDispBusy(0);
	keyPause();
}

void HAL_GPIO_EXTI_Callback(uint16_t pin) {
    switch(pin) {
		case KEY1_Pin:
            if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET) {

				if(start == 0) {
					start = 1;
				} else {
					if(getDispBusy() == 0) {
						if (pause) {
							pause = 0;
						} else {
							pause = 1;
						}
					} 	
				}
            }
        break;

        case KEY2_Pin:
            if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET) {
				st7026_vopDec();
            }
        break;

        case KEY3_Pin:
            if(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET) {
				st7026_vopAdd();
            }
        break;

        default:
            
        break;
	}
}
